package com.koron.plan.web.patrollineplan.service.impl;


import com.koron.plan.web.patrollineplan.bean.po.PlanLinePatrolRecord;
import com.koron.plan.web.patrollineplan.mapper.PlanLinePatrolRecordMapper;
import com.koron.plan.web.patrollineplan.service.PlanLinePatrolRecordService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 线路巡视数据记录项(PlanLinePatrolRecord)表服务实现类
 *
 * @author jzp
 * @since 2022-11-24 14:24:20
 */
@Service
public class PlanLinePatrolRecordServiceImpl implements PlanLinePatrolRecordService {

    /**
     * 通过ID查询单条数据
     *
     */
    @Override
    @TaskAnnotation("getById")
    public PlanLinePatrolRecord getById(SessionFactory factory, String id) {
        PlanLinePatrolRecordMapper mapper = factory.getMapper(PlanLinePatrolRecordMapper.class);
        return mapper.selectById(id);
    }

    /**
     * 查询所有数据
     *
     */
    @Override
    @TaskAnnotation("list")
    public List<PlanLinePatrolRecord> list(SessionFactory factory, PlanLinePatrolRecord planLinePatrolRecord) {
        PlanLinePatrolRecordMapper mapper = factory.getMapper(PlanLinePatrolRecordMapper.class);
        return mapper.selectList(planLinePatrolRecord);
    }

    @Override
    public List<PlanLinePatrolRecord> listByPlanId(SessionFactory factory, String planId) {
        PlanLinePatrolRecord planLinePatrolRecord = new PlanLinePatrolRecord();
        planLinePatrolRecord.setPlanId(planId);
        return list(factory, planLinePatrolRecord);
    }

    /**
     * 新增数据
     *
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, PlanLinePatrolRecord planLinePatrolRecord) {
        PlanLinePatrolRecordMapper mapper = factory.getMapper(PlanLinePatrolRecordMapper.class);
        return mapper.insertOrUpdate(planLinePatrolRecord) > 0;
    }

    @Override
    public boolean saveBatch(SessionFactory factory, List<PlanLinePatrolRecord> recordList) {
        if (CollectionUtils.isEmpty(recordList)) {
            return false;
        }
        PlanLinePatrolRecordMapper mapper = factory.getMapper(PlanLinePatrolRecordMapper.class);
        return mapper.insertOrUpdateBatch(recordList) > 0;
    }


    /**
     * 修改数据
     *
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, PlanLinePatrolRecord planLinePatrolRecord) {
        PlanLinePatrolRecordMapper mapper = factory.getMapper(PlanLinePatrolRecordMapper.class);
        return mapper.update(planLinePatrolRecord) > 0;
    }

    /**
     * 通过主键删除数据
     *
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        PlanLinePatrolRecordMapper mapper = factory.getMapper(PlanLinePatrolRecordMapper.class);
        return mapper.hidden(id) > 0;
    }

    @Override
    public boolean deleteBatchIds(SessionFactory factory, List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        PlanLinePatrolRecordMapper mapper = factory.getMapper(PlanLinePatrolRecordMapper.class);
        return mapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public boolean removeByPlanId(SessionFactory factory, String planId) {
        if (StringUtils.isEmpty(planId)) {
            return false;
        }
        List<PlanLinePatrolRecord> honorList = listByPlanId(factory, planId);
        return deleteBatchIds(factory, honorList.stream().map(PlanLinePatrolRecord::getId).collect(Collectors.toList()));
    }

}
